package com.zygh.policeasset.service;

import cn.hutool.core.date.DateUtil;
import com.zygh.policeasset.config.FileProperties;
import com.zygh.policeasset.mapper.DailyProjectMapper;
import com.zygh.policeasset.mapper.ProjectMapper;
import com.zygh.policeasset.mapper.UploadFileMapper;
import com.zygh.policeasset.pojo.UploadFile;
import com.zygh.policeasset.uploadFile.FileException;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.FileAttribute;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Service
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/service/UploadFileService.class */
public class UploadFileService {
    private final Path fileStorageLocation;
    @Autowired
    UploadFileMapper uploadFileMapper;
    @Value("${nginx.url}")
    String nginxUrl;

    @Autowired
    DailyProjectMapper dailyProjectMapper;

    @Autowired
    ProjectMapper projectMapper;

    @Autowired
    public UploadFileService(FileProperties fileProperties) {
        this.fileStorageLocation = Paths.get(fileProperties.getUploadDir(), new String[0]).toAbsolutePath().normalize();
        try {
            Files.createDirectories(this.fileStorageLocation, new FileAttribute[0]);
        } catch (Exception ex) {
            throw new FileException("Could not create the directory where the uploaded files will be stored.", ex);
        }
    }

    public String storeFile(MultipartFile file, String type, String name) {
        String url;
        String fileName = DateUtil.now().replaceAll("[[\\s-:punct:]]", "") + "" + StringUtils.cleanPath(file.getOriginalFilename());
        try {
            if (fileName.contains("..")) {
                throw new FileException("Sorry! Filename contains invalid path sequence " + fileName);
            }
            if (type.equals("project")) {
                url = "./file/uploads/" + type + "/" + fileName;
            } else {
                url = "./file/uploads/" + name + "/" + type + "/" + fileName;
            }
            File file1 = new File(url);
            System.out.println(url + "<<<<<<<<<<<<<<<");
            Files.copy(file.getInputStream(), file1.toPath(), StandardCopyOption.REPLACE_EXISTING);
            return fileName;
        } catch (IOException ex) {
            throw new FileException("Could not store file " + fileName + ". Please try again!", ex);
        }
    }

    public Resource loadFileAsResource(String fileName) {
        try {
            Resource resource = new UrlResource(this.fileStorageLocation.resolve(fileName).normalize().toUri());
            if (resource.exists()) {
                return resource;
            }
            throw new FileException("File not found " + fileName);
        } catch (MalformedURLException ex) {
            throw new FileException("File not found " + fileName, ex);
        }
    }

    public List<UploadFile> findByIdType(Integer id, String type, Integer fileType) {
        List<UploadFile> list = this.uploadFileMapper.findByIdType(id, type, fileType);
        for (UploadFile uploadFile : list) {
            uploadFile.setContent(this.nginxUrl + uploadFile.getContent());
        }
        return list;
    }

    public List<UploadFile> findInvoice(String assetType, String buildNum, Integer sectionId) {

        List<UploadFile> list = null;
        if ("da".equals(assetType)) {
            list = dailyProjectMapper.queryByProjectId(Integer.valueOf(buildNum));

        } else if ("cg".equals(assetType)) {
            list = projectMapper.queryPurchaseByProjectNo(buildNum);

        } else if ("jc".equals(assetType) || "znh".equals(assetType) || "zns".equals(assetType)) {

            list = projectMapper.queryProjectByProjectNo(buildNum, sectionId);
        }
        if (!StringUtils.isEmpty(list)) {
            for (UploadFile uploadFile : list) {
                uploadFile.setContent(this.nginxUrl + uploadFile.getContent());
            }
        }

        return list;
    }

    public Integer add(UploadFile params) {
        return this.uploadFileMapper.add(params);
    }

    public Integer remove(int id) {
        return this.uploadFileMapper.remove(id);
    }

    public UploadFile findById(int id) {
        return this.uploadFileMapper.findById(id);
    }
}